package com.bingo.demo.service;

import com.bingo.demo.DAO.EssayMapper;
import com.bingo.demo.pojo.Essay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class EssayService {

    @Autowired
    EssayMapper essayMapper;

    public List<Essay> findAll(){
        return essayMapper.findAll();
    }

    public void addEssay(Essay essay){
        //获取系统时间
        Date date = new Date();
        Timestamp ts=new Timestamp(date.getTime());
        essay.setPublishTime(ts);
        //获得一个随机地id
        String number=Integer.toString((int)(Math.random()*10000000));
        essay.setNumber(number);
        essayMapper.addEssay(essay);
    }

    public Essay findById(Integer id){
        return essayMapper.findById(id);
    }

    public List<Essay> findByAuthoruty(Integer authority){
        return essayMapper.findByAuthority(authority);
    }

    public List<Essay> findEssayByUserId(Integer id){
        List<Essay> list = essayMapper.findByUserId(id);
        return list;
    }

    public List<Essay> findEssayByTitle(String title){
        return essayMapper.findByTitle(title);
    }

    public Essay findEssayByNumber(String number){
        return essayMapper.findByNumber(number);
    }

    public void deleteEssayById(Integer id){
        essayMapper.deleteEssayById(id);
    }
}
